package com.spoto.oa.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.spoto.oa.dao.NoticeTypeDAO;
import com.spoto.oa.model.NoticeType;
import com.spoto.oa.util.DB;
/**
 * 
 * @Project: OA
 * @Description: 公告类型表DAO的具体实现类
 * @Author: LinYiSong
 * @Vesion: 
 * @Date: 2010-10-16~2010-10-16
 */
public class NoticeTypeDAOImpl implements NoticeTypeDAO {

	/**
	 * 取得所有的公告类型
	 * @return
	 */
	PreparedStatement pstm;
	public List<NoticeType> getAllNoticeType() {
		
		ResultSet rs = null;
		try {
			pstm = DB.getConn().prepareStatement("select name,no,id from noticeType");
			rs = pstm.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return resultSetToList(rs);
	}
	
	
	/**
	 * 封装ResultSet为list
	 * 
	 * @param rs
	 * @return
	 */
	private List<NoticeType> resultSetToList(ResultSet rs) {
		List<NoticeType> noticeTypes = new ArrayList<NoticeType>();

		try {
			while (rs.next()) {
				NoticeType noticeType = new NoticeType();
				noticeType.setId(rs.getInt("id"));
				noticeType.setName(rs.getString("name"));
				noticeType.setNo(rs.getInt("no"));
				noticeTypes.add(noticeType);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return noticeTypes;

	}

	/**
	 * 根据id删除公告类型
	 * @param id
	 * @throws SQLException 
	 */
	public void deleteTyepById(int id)  {
		try {
			pstm = DB.getConn().prepareStatement("delete from noticeType where id=?");
			pstm.setInt(1, id);
			pstm.executeUpdate();
		} catch (SQLException e) {
			System.out.println("删除公告类型不成功");
		}
		
	}


	/**
	 * 根据id取得对应的公告类型
	 * @param id
	 * @return
	 */
	public NoticeType getById(int id) {
		ResultSet rs = null;
		try {
			pstm = DB.getConn().prepareStatement("select name,no,id from noticeType where id=?");
			pstm.setInt(1, id);
			rs = pstm.executeQuery();
			return resultSetToList(rs).get(0);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
		
	}

	/**
	 * 更新公告类型
	 * @param noticeType
	 */
	public void update(NoticeType noticeType) {
		int id=noticeType.getId();
		String name=noticeType.getName();
		int no=noticeType.getNo();
		try {
			pstm = DB.getConn().prepareStatement("update noticeType set name=?,no=? where id=?");
			pstm.setString(1, name);
			pstm.setInt(2, no);
			pstm.setInt(3, id);
			pstm.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("更新公告类型不成功");
		}
	}

	/**
	 * 添加公告类型
	 * @param noticeType
	 */
	public void add(NoticeType noticeType) {
		String name=noticeType.getName();
		int no=noticeType.getNo();
		try {
			pstm = DB.getConn().prepareStatement("insert into noticeType(name,no) values(?,?);");
			pstm.setString(1, name);
			pstm.setInt(2, no);
			pstm.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("新增公告类型不成功");
		}
	}

	/**
	 * 根据公告类型名取得公告类型
	 * @param name
	 * @return
	 */
	public NoticeType getByName(String name) {
		ResultSet rs = null;
		try {
			pstm = DB.getConn().prepareStatement("select name,no,id from noticeType where name=?");
			pstm.setString(1, name);
			rs = pstm.executeQuery();
			return resultSetToList(rs).get(0);
		} catch (SQLException e) {
			e.printStackTrace();
		}catch(java.lang.IndexOutOfBoundsException e){
			return null;
		}
		return null;
	}

	/**
	 * 根据公告类型编号取得公告类型
	 * @param no
	 * @return
	 */
	public NoticeType getByNo(int no) {
		ResultSet rs = null;
		try {
			pstm = DB.getConn().prepareStatement("select name,no,id from noticeType where no=?");
			pstm.setInt(1, no);
			rs = pstm.executeQuery();
			return resultSetToList(rs).get(0);
		} catch (SQLException e) {
			e.printStackTrace();
		}catch(java.lang.IndexOutOfBoundsException e){
			return null;
		}
		return null;
	}

}
